package o2o.web.actions.code;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.ValidateUtil;
import o2o.config.annotation.LoginUser;
import o2o.dao.PO.code.DiscountExchangeCodeInfo;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.code.DiscountExchangeCodeVO;
import o2o.service.code.DiscountExchangeCodeService;
import o2o.web.actions.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: @Created by yangtk
 * @Date: @Date 2020/12/1 15:09
 * @Email: yangtongkuan@qq.com
 * @To change this template use File | Settings | File Templates.
 * @desc
 */
@RestController
public class DiscountExchangeCodeController {


    @Autowired
    private DiscountExchangeCodeService discountExchangeCodeService;

    /**
     * 增加
     */
    @RequestMapping("/admin/manager/discount/exchange/batch/create")
    public String discountExchangeBatchCreate(@LoginUser UserInfo user, DiscountExchangeCodeInfo info, Integer generateSize) {
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }
        if (info.getStartTime() == null || info.getEndTime() == null) {
            return AjaxResult.errorResult("有效期不能为空");
        }
        if (info.getDiscountRate() == null || info.getDiscountRate() < 0 || info.getDiscountRate() > 100) {
            return AjaxResult.errorResult("折扣比例设置不正确");
        }
        if (generateSize == null || generateSize < 1) {
            return AjaxResult.errorResult("生成个数不能为空");
        }
        info.setCreateUser(user.getUsername());
        try {
            discountExchangeCodeService.batchGenerateExchangeCode(info, generateSize);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        return AjaxResult.successResult();
    }


    /**
     * 删除
     */
    @RequestMapping("/admin/manager/discount/exchange/code/del")
    public String discountExchangeCodeDel(@LoginUser UserInfo user, String exchangeCode) {
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }
        try {
            discountExchangeCodeService.delDiscountExchangeCodeByCode(exchangeCode, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        return AjaxResult.successResult();
    }


    /**
     * 分页查询
     */
    @RequestMapping("/admin/manager/discount/exchange/list")
    public String discountExchangeList(@LoginUser UserInfo user, Long startTime, Long endTime, Integer isUse, Integer isExport, Integer page, Integer size) {
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 10;
        }
        if (isExport == null) {
            isExport = 0;
        }
        Object[] object = discountExchangeCodeService.searchByPage(startTime, endTime, isUse, isExport, page, size);
        if (isExport == 1) {
            String path = (String) object[0];
            return AjaxResult.successResult(path);
        }
        Long count = (Long) object[0];
        List<DiscountExchangeCodeInfo> list = (List<DiscountExchangeCodeInfo>) object[1];
        if (!ValidateUtil.isAbsList(list)) {
            return AjaxResult.successResult(null, 0);
        }
        List<DiscountExchangeCodeVO> codeVOList = list.stream().map(item -> item.convert2Vo()).collect(Collectors.toList());
        return AjaxResult.successResult(codeVOList, count);
    }

    @RequestMapping("/user/discount/exchange/code/detail")
    public String discountExchangeCodeDetail(String code) {
        DiscountExchangeCodeInfo codeByCode = discountExchangeCodeService.findDiscountExchangeCodeByCode(code);
        if (codeByCode == null) {
            return AjaxResult.successResult();
        }
        DiscountExchangeCodeVO exchangeCodeVO = codeByCode.convert2Vo();
        return AjaxResult.successResult(exchangeCodeVO);
    }


}
